﻿using ExChange.Model;
using System.Web.Mvc;
using System.Linq;
using System.Text;
using System;
using System.Diagnostics;
using System.Data.Entity;
using System.Collections.Generic;

namespace ExChange.Web.Controllers
{
    public class OrderController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 兑换统计页面
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderTongJi()
        {
            return View();
        }


        /// <summary>
        /// 兑换统计异步
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult GetData(int type = 0)
        {
            var list = from o in entity.Order select o;
            List<int> datalist = new List<int>();
            List<string> typeText = new List<string>();
            DateTime dt = DateTime.Now;  //当前时间  
            var text = "本年";
            if (type == 0 || type == (int)EnumHelper.Tongji.本年)
            {
                for (int i = 1; i <= 12; i++)
                {
                    datalist.Add(list.Where(f => f.CreateTime.Value.Year == DateTime.Now.Year && f.CreateTime.Value.Month == i).Count());
                }
                typeText = new List<string> { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
                //starttime = DateTime.Now.ToString("yyyy-MM-01");
                //var startdate = Convert.ToDateTime(starttime);
                //endtime = DateTime.Now.AddMonths(1).ToString("yyyy-MM-01");
                //var enddate = Convert.ToDateTime(endtime); 
            }
            if (type == (int)EnumHelper.Tongji.本周)
            {
                text = "本周";
                DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d")));  //本周周一  
                //DateTime endWeek = startWeek.AddDays(6);  //本周周日 
                for (int i = 0; i <= 6; i++)
                {
                    var day = startWeek.AddDays(i);
                    datalist.Add(list.Where(f => f.CreateTime.Value.Year == day.Year && f.CreateTime.Value.Month == day.Month && f.CreateTime.Value.Day == day.Day).Count());
                }
                typeText = new List<string> { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            }
            if (type == (int)EnumHelper.Tongji.本月)
            {
                text = "本月";
                datalist.Add(list.Where(f => f.CreateTime.Value.Year == DateTime.Now.Year && f.CreateTime.Value.Month == DateTime.Now.Month).Count());
                typeText = new List<string> { "本月" };
            }
            var data = new
            {
                data = datalist.ToArray(),
                type = typeText.ToArray(),
                text = text,
                total = datalist.Sum(),
            };
            return Json(data);
        }


        /// <summary>
        /// 订单状态
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderStateType()
        {
            var typeList = EnumHelper.EnumToList<EnumHelper.OrderState>();
            return Json(typeList, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetList(int page, int rows, string spmc = "", string hymc = "", string shr = "", string shdh = "", string shdz = "", string shyb = "", string starttime = "", string endtime = "", int hydj = 0, int zt = 0)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var list = from o in entity.Order
                       let s = entity.Member.FirstOrDefault(f => f.ID == o.UserID)
                       select new
                       {
                           o.ID,
                           o.UserID,
                           o.Phone,
                           o.Address,
                           o.ZipCode,
                           o.UserName,
                           o.CreateTime,
                           o.State,
                           o.WarePrice,
                           o.WareName,
                           o.SendTime,
                           MemberName = s == null ? "" : s.MemberName+"("+(s.Phone??s.loginname) +")",
                           MemberPhone = s == null ? "" : s.Phone,
                           MemberLoginname = s == null ? "" : s.loginname,
                           Memberlevel = o.MWareName,
                           hydjid = o.MWareID,
                       };

            if (!string.IsNullOrEmpty(spmc))
            {
                list = list.Where(f => f.WareName.Contains(spmc));
            }
            if (!string.IsNullOrEmpty(hymc))
            {
                list = list.Where(f => f.MemberName.Contains(hymc) || f.MemberPhone.Contains(hymc) || f.MemberLoginname.Contains(hymc));
            }
            if (!string.IsNullOrEmpty(shr))
            {
                list = list.Where(f => f.UserName.Contains(shr));
            }
            if (!string.IsNullOrEmpty(shdh))
            {
                list = list.Where(f => f.Phone.Contains(shdh));
            }
            if (!string.IsNullOrEmpty(shdz))
            {
                list = list.Where(f => f.Address.Contains(shdz));
            }
            if (!string.IsNullOrEmpty(shyb))
            {
                list = list.Where(f => f.ZipCode.Contains(shyb));
            }
            if (hydj != 0)
            {
                list = list.Where(f => f.hydjid == hydj);
            }
            if (zt != 0)
            {
                list = list.Where(f => f.State == zt);
            }
            if (!string.IsNullOrEmpty(starttime))
            {
                DateTime stime = Convert.ToDateTime(starttime);
                list = list.Where(o => o.CreateTime >= stime);
            }
            if (!string.IsNullOrEmpty(endtime))
            {
                DateTime etime = Convert.ToDateTime(endtime);
                list = list.Where(o => o.CreateTime <= etime);
            }
            var totalcount = list.Count();
            var data = list.OrderByDescending(f => f.CreateTime).Skip((page - 1) * rows).Take(rows).ToList().Select(f => new
            {
                Memberlevel = f.Memberlevel,
                f.Address,
                CreateTime = f.CreateTime,
                f.Phone,
                f.ID,
                f.State,
                StateText = ((EnumHelper.OrderState)f.State) + "",
                f.UserID,
                MemberName = f.MemberName,
                f.UserName,
                f.WareName,
                f.WarePrice,
                f.ZipCode,
            });
            sw.Stop();
            var time = sw.Elapsed;
            return Json(new { rows = data, total = totalcount }, 1);
        }

        /// <summary>
        /// 修改发送时间,发送状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult UpdateSendState(int id)
        {
            var order = entity.Order.FirstOrDefault(f => f.ID == id);
            order.State = (int)EnumHelper.OrderState.已发货;
            order.SendTime = DateTime.Now;
            entity.Entry<Order>(order).State = EntityState.Modified;
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }
    }
}
